//
//  BaseModel.m
//  Baseproject
//
//  Created by gullmei on 15/9/11.
//  Copyright (c) 2015年 黄定坤. All rights reserved.
//

#import "BaseModel.h"
#include <objc/runtime.h>
@implementation BaseModel

-(id)init
{
    self = [super init];
    if (self) {
        u_int count;
        objc_property_t *properties =class_copyPropertyList([self class], &count);
        for (int i = 0; i<count; i++)
        {
            objc_property_t property = properties[i];
            const char* char_f =property_getName(property);
            const char* char_f1 =   property_getAttributes(property);
            NSString *propertyName = [NSString stringWithUTF8String:char_f];
            NSString *attributeString = [NSString stringWithCString:char_f1];
            NSString * substring = @"NSArray";
            
            if ([attributeString rangeOfString:substring].location != NSNotFound)
            {
                NSArray *  array1 = [[NSArray alloc] init];
                [self setValue:array1 forKey:(NSString *)propertyName];
            }else
            {
                [self setValue:@"" forKey:(NSString *)propertyName];
            }
            
        }
        free(properties);
    }
    return self;  //如果失败返回，self的值是nil，返回的nil
    
}
- (void)initData
{
    //初始化单例数据
}
+(BOOL)propertyIsOptional:(NSString *)propertyName{
    return YES;
}
#pragma mark - JSON数据编解码
#pragma
+(BaseModel*)decodeAndSaveData:(id)data
{
    
    BaseModel * model = [[self alloc] initWithDictionary:data error:nil ];
    return model;
}
#pragma mark－存储编解码
//- (void)encodeWithCoder:(NSCoder *)encoder {
//    Class cls = [self class];
//    while (cls != [NSObject class]) {
//        unsigned int numberOfIvars = 0;
//        Ivar* ivars = class_copyIvarList(cls, &numberOfIvars);
//        for(const Ivar* p = ivars; p < ivars+numberOfIvars; p++)
//        {
//            Ivar const ivar = *p;
//            const char *type = ivar_getTypeEncoding(ivar);
//            NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
//            if([key isEqualToString:@"_delegate"])
//            {
//                continue;
//            }
//            id value = [self valueForKey:key];
//            if (value) {
//                switch (type[0]) {
//                    case _C_STRUCT_B: {
//                        NSUInteger ivarSize = 0;
//                        NSUInteger ivarAlignment = 0;
//                        NSGetSizeAndAlignment(type, &ivarSize, &ivarAlignment);
//                        NSData *data = [NSData dataWithBytes:(const char *)self + ivar_getOffset(ivar)
//                                                      length:ivarSize];
//                        [encoder encodeObject:data forKey:key];
//                    }
//                        break;
//                    default:
//                        
//                        if ([value conformsToProtocol:@protocol(NSCoding)])
//                        {
//                            [encoder encodeObject:value forKey:key];
//                        }else {
//                            //assert(0); // crash for debug
//                        }
//                        
//                        break;
//                }
//            }
//        }
//        if (ivars) {
//            free(ivars);
//        }
//        
//        cls = class_getSuperclass(cls);
//    }
//}
//
//- (id)initWithCoder:(NSCoder *)decoder {
//    
//    self = [super init];
//    
//    if (self) {
//        Class cls = [self class];
//        while (cls != [NSObject class]) {
//            unsigned int numberOfIvars = 0;
//            Ivar* ivars = class_copyIvarList(cls, &numberOfIvars);
//            
//            for(const Ivar* p = ivars; p < ivars+numberOfIvars; p++)
//            {
//                Ivar const ivar = *p;
//                const char *type = ivar_getTypeEncoding(ivar);
//                const char *name = ivar_getName(ivar);
//                NSString *key = [NSString stringWithUTF8String:name];
//                id value = [decoder decodeObjectForKey:key];
//                if (value) {
//                    switch (type[0]) {
//                        case _C_STRUCT_B: {
//                            NSUInteger ivarSize = 0;
//                            NSUInteger ivarAlignment = 0;
//                            NSGetSizeAndAlignment(type, &ivarSize, &ivarAlignment);
//                            NSData *data = [decoder decodeObjectForKey:key];
//                            char *sourceIvarLocation = (char*)self+ ivar_getOffset(ivar);
//                            [data getBytes:sourceIvarLocation length:ivarSize];
//                            memcpy((char *)self + ivar_getOffset(ivar), sourceIvarLocation, ivarSize);
//                        }
//                            break;
//                        default:
//                            [self setValue:value forKey:key];
//                            
//                            break;
//                    }
//                }
//            }
//            
//            if (ivars) {
//                free(ivars);
//            }
//            cls = class_getSuperclass(cls);
//        }
//    }
//    
//    return self;
//}
#pragma mark- 打印model
+(NSMutableArray*)GetStringNeedPrint:(id)dic
{
  
    NSDictionary * dic1 = (NSDictionary*)dic;
    
    NSArray* arr = [dic1 allKeys];
    NSMutableArray * strArray = [[NSMutableArray alloc]  init];
    NSMutableArray * strTempArray =[[NSMutableArray alloc] init];
    NSString * str =@"@interface **model : BaseModel\n" ;
    
    for (int  i = 0 ; i < arr.count ; i++)
    {
        id object = [dic1 objectForKey:[arr objectAtIndex:i]];
      /*  if(![object isKindOfClass:[NSArray class]])
        {
            str = [str stringByAppendingFormat:@"@property(nonatomic,strong) NSString<Optional> * %@;\n",[arr objectAtIndex:i]];
        }else
        {
            
            str = [str stringByAppendingFormat:@"@property(nonatomic,strong) NSArray<Optional> * %@;\n",[arr objectAtIndex:i]];
            NSArray * array = (NSArray*)object;
            if(array != nil && array.count >= 1)
            {
            NSDictionary * dic1 = [array objectAtIndex:0];
            NSMutableArray * array1 = [BaseModel GetStringNeedPrint:dic1];
            for (int j =0; j< array1.count; j++) {
                [strTempArray addObject:[array1 objectAtIndex:j]];
            }
            }
        }*/
        if([object isKindOfClass:[NSArray class]])
        {
            str = [str stringByAppendingFormat:@"@property(nonatomic,strong) NSArray<Optional> * %@;\n",[arr objectAtIndex:i]];
            NSArray * array = (NSArray*)object;
            if(array != nil && array.count >= 1)
            {
                NSDictionary * dic1 = [array objectAtIndex:0];
                NSMutableArray * array1 = [BaseModel GetStringNeedPrint:dic1];
                for (int j =0; j< array1.count; j++) {
                    [strTempArray addObject:[array1 objectAtIndex:j]];
                }
            }

        }else if([object isKindOfClass:[NSDictionary class]])
        {
            str = [str stringByAppendingFormat:@"@property(nonatomic,strong) **object<Optional> * %@;\n",[arr objectAtIndex:i]];
 
                NSDictionary * dic1 = (NSDictionary*)object;
                NSMutableArray * array1 = [BaseModel GetStringNeedPrint:dic1];
                for (int j =0; j< array1.count; j++) {
                    [strTempArray addObject:[array1 objectAtIndex:j]];
                }
        }else
        {
                    str = [str stringByAppendingFormat:@"@property(nonatomic,strong) NSString<Optional> * %@;\n",[arr objectAtIndex:i]];
        }
    }
    str = [str stringByAppendingString:@"@end\n\n\n"];
    
    str = [str stringByAppendingString:@"@implementation **model\n@synthesize  "];
    
    for (int  i = 0 ; i < arr.count ; i++)
    {
        if(i != arr.count -1)
        {
            str = [str stringByAppendingFormat:@"%@, ",[arr objectAtIndex:i]];
        }else
        {
            str = [str stringByAppendingFormat:@"%@",[arr objectAtIndex:i]];
        }
        
    }
    str = [str stringByAppendingString:@";\n@end"];
    for (int j =0; j< strTempArray.count; j++) {
        [strArray addObject:[strTempArray objectAtIndex:j]];
    }
    [strArray addObject:str];
    
    return strArray;
}
+(void)printModel:(id)dic
{
    NSMutableArray * arrayPrint  = [BaseModel GetStringNeedPrint:dic];
    for(id item in arrayPrint)
    {
        NSString * str = (NSString*)item;
        DLog(@"%@\n\n\n",str);
    }
}

@end
